<template>
  <el-dialog :title="fromData.id?'修改目录':'新增目录'" :visible="showDialog" width="400px" @close="closeDialog" @open="openDialog">
    <el-form label-width="80px" :model="fromData" :rules="rules" ref="from">
      <el-form-item label="所属学科" v-if="!$route.query.name">
        <el-select v-model="fromData.subjectID" style="width:100%">
          <el-option v-for="item in subjectList" :key="item.id" :label="item.subjectName" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="tagName">
        <el-input v-model="fromData.tagName" placeholder="请输入名录名称"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="publish">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { list } from '@/api/hmmm/subjects.js'
import { add, detail, update } from '@/api/hmmm/tags.js'
export default {
  name: 'tagsAdd',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      fromData: {
        subjectID: '',
        tagName: ''
      },
      subjectList: [],
      rules: {
        tagName: [{ required: true, message: '请输入目录名称～～～', trigger: 'blur' }]
      }
    }
  },
  methods: {
    closeDialog () {
      this.$emit('closeDialog')
      this.$refs.from.resetFields()
      this.fromData = {
        subjectID: '',
        tagName: ''
      }
    },
    async openDialog () {
      const { data: res } = await list({ page: 1, pagesize: 100 })
      this.subjectList = res.items
    },
    publish () {
      this.$refs.from.validate(async (valid) => {
        if (!valid) return
        let res = {}
        if (this.fromData.id) {
          res = await update(this.fromData)
        } else {
          res = await add(this.fromData)
        }
        if (res.status === 200) {
          this.$message.success(`${this.fromData.id ? '修改' : '添加'}成功`)
          this.closeDialog()
        }
      })
    },
    async getTags (id) {
      const { data: res } = await detail(id)
      this.fromData = res
    }
  }
}
</script>

<style scoped lang='less'></style>
